<!--
 * @Descripttion: 点赞集福活动列表
 * @Author: xiao li
 * @Date: 2020-12-07 11:40:09
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-18 13:55:27
-->
<template>
  <div class="lb-goods-classify">
    <top-nav />
    <div class="page-main">
      <lb-button
        type="primary"
        icon="el-icon-plus"
        @click="$router.push(`${appPath}/payclass/editAtv`)"
        >{{ $t('menu.PayclassAtvAdd') }}</lb-button
      >
      <div class="space-lg"></div>
      <el-row class="page-search-form">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item label="输入查询" prop="title">
            <el-input
              v-model="searchForm.title"
              placeholder="请输入课程名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <lb-button
              size="medium"
              type="primary"
              opType="add"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              @click="resetForm"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="class_name" label="课程名称"> </el-table-column>
        <el-table-column prop="num" label="集福人数"> </el-table-column>
        <el-table-column prop="price" label="活动价格">
          <template slot-scope="scope">
            {{ scope.row.price_type == 0 ? '免费' : `¥${scope.row.price}` }}
          </template>
        </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="status" label="是否上架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateItem(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  $router.push(`${appPath}/payclass/editAtv?id=${scope.row.id}`)
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="
                  $router.push(
                    `${appPath}/payclass/atvDetail?id=${scope.row.id}`
                  )
                "
                >参与情况</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      total: 0,
      searchForm: {
        page: 1,
        limit: 10,
        title: ''
      },
      tableData: [],
      multipleSelection: [],
      showTable: true
    }
  },
  activated () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''

    this.getTableDataList()
  },
  methods: {
    /**
     * @method: 重置表单
     */
    resetForm () {
      this.$refs['searchForm'].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 获取列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.payclassAtvList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 删除
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    },
    /**
     * @method 上下架/删除
     */
    async updateItem (id, status) {
      this.$api.payclassAtvUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-goods-classify {
  width: 100%;
  .page-main {
    padding: 20px;
    .el-row {
      margin-bottom: 20px;
      .el-button {
        margin-right: 24px;
        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
